package com.snail.poker.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.type.Type;

import com.snail.poker.dao.BaseDao;
import com.snail.poker.dao.InningsDao;
import com.snail.poker.model.Innings;
import com.snail.poker.web.PageContext;
import com.snail.poker.web.bean.PageBean;

/**
 * 牌局中的各概率等数据存储接口的实现
 * 
 * @author Snail
 * 
 */
public class InningsDaoImpl extends BaseDao implements InningsDao {

	@Override
	public void addInnings(Innings innings) throws Exception {
		// TODO Auto-generated method stub
		super.getHibernateTemplate().save(innings);
	}

	@Override
	public void delInnings(int inning) throws Exception {
		// TODO Auto-generated method stub
		Innings innings = this.getInnings(inning);
		if (null == innings) {
			throw new Exception("未发现标识编号为[ " + inning + " ]的相关数据记录.");
		}
		super.getHibernateTemplate().delete(innings);
	}

	@Override
	public Innings getInnings(int inning) throws Exception {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().get(Innings.class, inning);
	}

	@Override
	public PageBean findInnings(Innings innings, Double[] among) {
		// TODO Auto-generated method stub
		if (null == innings || null == among) {
			return this.queryInnings();
		}
		StringBuffer hql = new StringBuffer("from Innings i where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != innings.getMemberName()
				&& !innings.getMemberName().isEmpty()) {
			hql.append(" and i.memberName like ? ");
			params.add("%" + innings.getMemberName() + "%");
		}
		if (null != among[0]) {
			hql.append(" and i.insure >= ? ");
			params.add(among[0]);
		}
		if (null != among[1]) {
			hql.append(" and i.insure <= ? ");
			params.add(among[1]);
		}
		if (0 < innings.getLose()) {
			hql.append(" and i.lose >= ? ");
			params.add(innings.getLose());
		}
		if (0 < innings.getOdds()) {
			hql.append(" and i.odds >= ? ");
			params.add(innings.getOdds());
		}
		if (0 < innings.getOppTie()) {
			hql.append(" and i.oppTie >= ? ");
			params.add(innings.getOppTie());
		}
		if (0 < innings.getPlayers()) {
			hql.append(" and i.players >= ? ");
			params.add(innings.getPlayers());
		}
		if (0 < innings.getTie()) {
			hql.append(" and i.tie >= ? ");
			params.add(innings.getTie());
		}
		if (0 < innings.getWin()) {
			hql.append(" and i.win >= ? ");
			params.add(innings.getWin());
		}
		if (null != PageContext.getBeginTime()) {
			hql.append(" and i.createTime >= ? ");
			params.add(PageContext.getBeginTime());
		}
		if (null != PageContext.getEndTime()) {
			hql.append(" and i.createTime <= ? ");
			params.add(PageContext.getEndTime());
		}
		hql.append(" order by i.createTime desc ");

		return super.getPagination(hql.toString(), params.toArray());
	}

	@Override
	public List<Innings> searchInnings(Innings innings, Double[] among) {
		// TODO Auto-generated method stub
		if (null == innings || null == among) {
			return super.getHibernateTemplate().find(
					"from Innings i order by i.createTime desc");
		}
		StringBuffer hql = new StringBuffer("from Innings i where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		List<Type> types = new ArrayList<Type>();
		if (null != innings.getMemberName()
				&& !innings.getMemberName().isEmpty()) {
			hql.append(" and i.memberName like ? ");
			params.add("%" + innings.getMemberName() + "%");
			types.add(Hibernate.STRING);
		}
		if (null != among[0]) {
			hql.append(" and i.insure >= ? ");
			params.add(among[0]);
			types.add(Hibernate.DOUBLE);
		}
		if (null != among[1]) {
			hql.append(" and i.insure <= ? ");
			params.add(among[1]);
			types.add(Hibernate.DOUBLE);
		}
		if (0 < innings.getLose()) {
			hql.append(" and i.lose >= ? ");
			params.add(innings.getLose());
			types.add(Hibernate.DOUBLE);
		}
		if (0 < innings.getOdds()) {
			hql.append(" and i.odds >= ? ");
			params.add(innings.getOdds());
			types.add(Hibernate.DOUBLE);
		}
		if (0 < innings.getOppTie()) {
			hql.append(" and i.oppTie >= ? ");
			params.add(innings.getOppTie());
			types.add(Hibernate.DOUBLE);
		}
		if (0 < innings.getPlayers()) {
			hql.append(" and i.players >= ? ");
			params.add(innings.getPlayers());
			types.add(Hibernate.INTEGER);
		}
		if (0 < innings.getTie()) {
			hql.append(" and i.tie >= ? ");
			params.add(innings.getTie());
			types.add(Hibernate.DOUBLE);
		}
		if (0 < innings.getWin()) {
			hql.append(" and i.win >= ? ");
			params.add(innings.getWin());
			types.add(Hibernate.DOUBLE);
		}
		if (null != PageContext.getBeginTime()) {
			hql.append(" and i.createTime >= ? ");
			params.add(PageContext.getBeginTime());
			types.add(Hibernate.DATE);
		}
		if (null != PageContext.getEndTime()) {
			hql.append(" and i.createTime <= ? ");
			params.add(PageContext.getEndTime());
			types.add(Hibernate.DATE);
		}
		hql.append(" order by i.createTime desc ");
		Type[] t = {};
		return super.getSession().createQuery(hql.toString())
				.setParameters(params.toArray(), types.toArray(t)).list();
	}

	@Override
	public PageBean queryInnings() {
		// TODO Auto-generated method stub
		String hql = "from Innings i order by i.createTime desc ";
		return super.getPagination(hql);
	}

	@Override
	public void modifyInnings(Innings innings) {
		// TODO Auto-generated method stub
		String hql = "update Innings i set i.memberName = ? , i.odds = ? , i.insure = ? where i.inningSn = ?";
		super.getSession().createQuery(hql)
				.setParameter(0, innings.getMemberName())
				.setParameter(1, innings.getOdds())
				.setParameter(2, innings.getInsure())
				.setParameter(3, innings.getInningSn()).executeUpdate();
	}

}
